﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_ucControl_ucNewItem : System.Web.UI.UserControl
{
    TblSupplierDAO supplierDAO = new TblSupplierDAO(Global.CONNECTION_STRING);
    TblChildMenuDAO chilDAO = new TblChildMenuDAO(Global.CONNECTION_STRING);
    TblItemsDAO itemDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblItemImageDAO imageDAO = new TblItemImageDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];

        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == "" || Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            if (id == null || id == "")
            {
                LoadSupplier();
                LoadMenuChild();
            }
            else
            {
                LoadItemById();
                LoadImgItemByID();
                LoadSupplier();
                LoadMenuChild();
            }
        }
    }

    private void LoadImgItemByID()
    {
        try
        {
            string id = Request.QueryString["id"];
            grdImgItems.DataSource = itemDAO.GetImgItems(Global.ConvertToInt(id));
            grdImgItems.DataBind();
        }
        catch(Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
    }

    private void LoadItemById()
    {
        LoadImgItemByID();
        LoadMenuChild();

        string id = Request.QueryString["id"];

        TblItems Item = itemDAO.GetItemByID(Convert.ToInt32(id));
        txtItemName.Text = Item.ItemName;
        txtMaSP.Text = Item.ItemNumber;
        txtPrice.Text = Convert.ToDecimal(Item.Price).ToString();
        txtFeatures.Content = Server.HtmlDecode(Item.Features);
        txtDescription.Content = Server.HtmlDecode(Item.ItemDescription);
        txtSpecification.Content = Server.HtmlDecode(Item.Specification);

        drMenuChild.SelectedValue = Convert.ToInt32(Item.IDMenuChild).ToString();
        drSuppliers.SelectedValue = Convert.ToInt32(Item.IDSupplie).ToString();


        
    }


    private void LoadSupplier()
    {
        try
        {
            drSuppliers.DataSource = supplierDAO.GetAll();
            drSuppliers.DataTextField = "Suppliers";
            drSuppliers.DataValueField = "IDSupplie";
            drSuppliers.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
    }

    private void LoadMenuChild()
    {
        try
        {
            drMenuChild.DataSource = chilDAO.GetMenuChild();
            drMenuChild.DataTextField = "MenuChilName";
            drMenuChild.DataValueField = "IDMenuChild";
            drMenuChild.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        bool flag = false;
        string id = Request.QueryString["id"];
        try
        {
            TblItems Item = new TblItems();
            Item.ItemName = txtItemName.Text;
            Item.ItemNumber = txtMaSP.Text;
            Item.ItemDescription = Server.HtmlEncode(txtDescription.Content);
            Item.Features = Server.HtmlEncode(txtFeatures.Content);
            Item.Specification = Server.HtmlEncode(txtSpecification.Content);

            Item.IDMenuChild = Convert.ToInt32(drMenuChild.SelectedValue);
            Item.IDSupplie = Convert.ToInt32(drSuppliers.SelectedValue);

            Item.Price = 0;

            if (id == null || id == "")
            {
                Item.IDItem = 0;
                itemDAO.InsUpdItem(Item, 1);
            }
            else
            {
                Item.IDItem = Convert.ToInt32(id);
                itemDAO.InsUpdItem(Item, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            if (id == null || id == "")
                Response.Redirect("~/Admin/ThemSanPham.aspx");
            else
                //Response.Redirect("~/Admin/AddItem.aspx?id=" + id);
                Response.Redirect("~/Admin/ThemSanPham.aspx");
        }
    }
    protected void grdImgItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        button.CommandArgument = ((TblItemImage)e.Row.DataItem).ID.ToString();
                        button.OnClientClick = "if (!confirm('Bạn có thật sự muốn xóa không ?')) return false;";
                    }
                }
            }
        }
    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblItemImage imgItem = new TblItemImage();

            imgItem.ID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);

            imgItem = imageDAO.GetItemImageByID(Global.ConvertToInt(id), idXoa);
            itemDAO.DeleteImgItem(imgItem);

            string filename = imgItem.ImageURL;
            if (File.Exists(Server.MapPath("~\\Admin\\images\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\images\\Items\\" + filename));
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadImgItemByID();
        }

    }


    protected void btnAddImage_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {

            string path = Server.MapPath("~\\Admin\\images\\Items\\");
            string filename = id + "_" + upImage.FileName;

            TblItemImage imgItem = new TblItemImage();
            // lblMessage.Text = path;
            if (File.Exists(path + "\\" + filename))
            {
                lbMessage.Text = "Hình ảnh này đã tồn tại.Hãy chọn một hình ảnh khác";
                return;
            }
            upImage.SaveAs(path + "\\" + filename);
            imgItem.ImageURL = filename;
            imgItem.IDItem = Convert.ToInt32(id);

            itemDAO.InsImageItem(imgItem, 1);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            if (id == null || id == "")
                Response.Redirect("~/Admin/ThemSanPham.aspx?id=" + id);
            else
                Response.Redirect("~/Admin/ThemSanPham.aspx?id=" + id);
        }
    }
}
